package com.bfly.mall.system.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.common.page.Pager;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.context.PagerThreadLocal;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.core.security.ActionModel;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.PermissionCheck;
import com.bfly.mall.system.entity.Dictionary;
import com.bfly.mall.system.dto.DictionaryFormDTO;
import com.bfly.mall.system.dto.DictionaryListDTO;
import com.bfly.mall.system.dto.DictionaryNameDTO;
import com.bfly.mall.system.service.IDictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2018/12/14 11:21
 */
@RestController
@RequestMapping(value = "/manage/dictionary")
public class DictionaryController extends BaseController {

    @Autowired
    private IDictionaryService dictionaryService;

    /**
     * 数据字典列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2018/12/14 11:22
     */
    @GetMapping(value = "/list")
    @ActionModel(value = "获取数据字典集合", recordLog = false)
    public void listDictionary(HttpServletRequest request, HttpServletResponse response) {
        PagerThreadLocal.set(request);

        Map<String, Object> param = new HashMap<>(1);
        String type = request.getParameter("type");
        if (StringUtils.isNotBlank(type)) {
            param.put("type", type);
        }
        Map<String, Boolean> sort = new HashMap<>(1);
        sort.put("seq", true);
        Pager<Dictionary> pager = dictionaryService.getPage(param, null, sort, null);

        Pager<DictionaryListDTO> dtoPager = new Pager<>(pager.getPageNo(), pager.getPageSize(), pager.getTotalCount());

        if (pager.getData() != null) {
            List<DictionaryListDTO> list = new ArrayList<>();
            for (Dictionary dictionary : pager.getData()) {
                DictionaryListDTO dto = new DictionaryListDTO();
                BeanUtils.copyProperties(dictionary, dto);
                list.add(dto);
            }
            dtoPager.setData(list);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dtoPager));
    }

    /**
     * 根据类型查询所有的数据字典
     *
     * @author 长沙商脉科技有限公司
     * @date 2019/7/22 11:24
     */
    @GetMapping(value = "/type")
    @ActionModel(value = "根据类型查询数据字典", recordLog = false)
    @PermissionCheck(required = false)
    public void getAllDictionaryByType(@RequestParam("type") String type, HttpServletResponse response) {
        Map<String, Object> exactMap = new HashMap<>(1);
        exactMap.put("type", type);
        List<Dictionary> list = dictionaryService.getList(exactMap);
        List<DictionaryNameDTO> names = new ArrayList<>();
        for (Dictionary dictionary : list) {
            DictionaryNameDTO nameDTO = new DictionaryNameDTO();
            BeanUtils.copyProperties(dictionary, nameDTO);
            names.add(nameDTO);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(names));
    }

    /**
     * 添加数据字典
     *
     * @author 长沙商脉科技有限公司
     * @date 2018/12/14 11:32
     */
    @PostMapping(value = "/add")
    @ActionModel(value = "新增数据字典")
    public void addDictionary(@RequestBody @Valid DictionaryFormDTO dictionary, BindingResult result, HttpServletResponse response) {
        validData(result);
        dictionaryService.save(dictionary);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 修改数据字典
     *
     * @author 长沙商脉科技有限公司
     * @date 2018/12/14 11:33
     */
    @PostMapping(value = "/edit")
    @ActionModel(value = "修改数据字典")
    public void editDictionary(@RequestBody @Valid DictionaryFormDTO dictionary, BindingResult result, HttpServletResponse response) {
        validData(result);
        dictionaryService.edit(dictionary);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 删除数据字典
     *
     * @author 长沙商脉科技有限公司
     * @date 2018/12/14 11:37
     */
    @PostMapping(value = "/delete")
    @ActionModel(value = "删除数据字典")
    public void removeDictionary(HttpServletResponse response, @RequestBody Integer... dirId) {
        dictionaryService.remove(dirId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 修改数据字典状态
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/6/7 16:38
     */
    @GetMapping(value = "/enabled")
    @ActionModel(value = "修改数据字典状态")
    public void editEnabled(HttpServletResponse response, @RequestParam("dictionaryId") int dictionaryId, @RequestParam("enabled") boolean enabled) {
        Dictionary dir = dictionaryService.get(dictionaryId);
        if (dir == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "数据字典不存在!");
        }
        dir.setEnabled(enabled);
        dictionaryService.edit(dir);
    }

    /**
     * 数据字典详情
     *
     * @author 长沙商脉科技有限公司
     * @date 2018/12/14 11:38
     */
    @GetMapping(value = "/{dictionaryId}")
    @ActionModel(value = "获取数据字典详情", recordLog = false)
    public void viewDictionary(@PathVariable("dictionaryId") int dictionaryId, HttpServletResponse response) {
        Dictionary dictionary = dictionaryService.get(dictionaryId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dictionary));
    }

    /**
     * 获得数据字典类型
     *
     * @author 长沙商脉科技有限公司
     * @date 2018/12/14 11:39
     */
    @RequestMapping(value = "/types")
    @ActionModel(value = "获取数据字典类型集合", recordLog = false)
    @PermissionCheck(required = false)
    public void listDictionaryType(HttpServletResponse response) {
        List<String> types = dictionaryService.getTypes();
        ResponseUtil.writeJson(response, ResponseData.getSuccess(types));
    }
}
